home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectMusic / PlayMotif / frmPlayMotif.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  15.3 KB  |  438 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmPlayMotif 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "vb PlayMotif"
  6.    ClientHeight    =   4365
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   5955
  10.    Icon            =   "frmPlayMotif.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   4365
  15.    ScaleWidth      =   5955
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin MSComDlg.CommonDialog cdlOpen 
  18.       Left            =   5160
  19.       Top             =   1080
  20.       _ExtentX        =   847
  21.       _ExtentY        =   847
  22.       _Version        =   393216
  23.       DialogTitle     =   "Open Segment File"
  24.    End
  25.    Begin VB.TextBox txtStatus 
  26.       BackColor       =   &H8000000F&
  27.       Height          =   315
  28.       Left            =   1320
  29.       Locked          =   -1  'True
  30.       TabIndex        =   16
  31.       Top             =   600
  32.       Width           =   4515
  33.    End
  34.    Begin VB.TextBox txtSegment 
  35.       BackColor       =   &H8000000F&
  36.       Height          =   315
  37.       Left            =   1320
  38.       Locked          =   -1  'True
  39.       TabIndex        =   14
  40.       Top             =   180
  41.       Width           =   4515
  42.    End
  43.    Begin VB.OptionButton optMeasure 
  44.       Caption         =   "Measure"
  45.       Height          =   255
  46.       Left            =   4800
  47.       TabIndex        =   13
  48.       Top             =   3600
  49.       Value           =   -1  'True
  50.       Width           =   975
  51.    End
  52.    Begin VB.OptionButton optBeat 
  53.       Caption         =   "Beat"
  54.       Height          =   255
  55.       Left            =   4020
  56.       TabIndex        =   12
  57.       Top             =   3600
  58.       Width           =   675
  59.    End
  60.    Begin VB.OptionButton optGrid 
  61.       Caption         =   "Grid"
  62.       Height          =   255
  63.       Left            =   3180
  64.       TabIndex        =   11
  65.       Top             =   3600
  66.       Width           =   735
  67.    End
  68.    Begin VB.OptionButton optImmediate 
  69.       Caption         =   "Immediate"
  70.       Height          =   255
  71.       Left            =   2040
  72.       TabIndex        =   10
  73.       Top             =   3600
  74.       Width           =   1035
  75.    End
  76.    Begin VB.OptionButton optDefault 
  77.       Caption         =   "Default"
  78.       Height          =   255
  79.       Left            =   1080
  80.       TabIndex        =   9
  81.       Top             =   3600
  82.       Width           =   855
  83.    End
  84.    Begin VB.ListBox lstMotif 
  85.       Height          =   1815
  86.       Left            =   60
  87.       TabIndex        =   7
  88.       Top             =   1680
  89.       Width           =   5775
  90.    End
  91.    Begin VB.CheckBox chkLoop 
  92.       Caption         =   "Loop Segment"
  93.       Height          =   195
  94.       Left            =   120
  95.       TabIndex        =   5
  96.       Top             =   1140
  97.       Width           =   1395
  98.    End
  99.    Begin VB.CommandButton cmdStop 
  100.       Caption         =   "&Stop"
  101.       Height          =   315
  102.       Left            =   2700
  103.       TabIndex        =   4
  104.       Top             =   1080
  105.       Width           =   1095
  106.    End
  107.    Begin VB.CommandButton cmdPlay 
  108.       Caption         =   "&Play"
  109.       Height          =   315
  110.       Left            =   1560
  111.       TabIndex        =   3
  112.       Top             =   1080
  113.       Width           =   1095
  114.    End
  115.    Begin VB.CommandButton cmdExit 
  116.       Caption         =   "E&xit"
  117.       Height          =   315
  118.       Left            =   4740
  119.       TabIndex        =   2
  120.       Top             =   3960
  121.       Width           =   1095
  122.    End
  123.    Begin VB.CommandButton cmdPlayMotif 
  124.       Caption         =   "Play &Motif"
  125.       Height          =   315
  126.       Left            =   60
  127.       TabIndex        =   1
  128.       Top             =   3960
  129.       Width           =   1095
  130.    End
  131.    Begin VB.CommandButton cmdSegment 
  132.       Caption         =   "Segment &File"
  133.       Default         =   -1  'True
  134.       Height          =   315
  135.       Left            =   120
  136.       TabIndex        =   0
  137.       Top             =   180
  138.       Width           =   1095
  139.    End
  140.    Begin VB.Label Label1 
  141.       Alignment       =   1  'Right Justify
  142.       BackStyle       =   0  'Transparent
  143.       Caption         =   "Status:"
  144.       Height          =   195
  145.       Index           =   2
  146.       Left            =   120
  147.       TabIndex        =   15
  148.       Top             =   660
  149.       Width           =   1035
  150.    End
  151.    Begin VB.Label Label1 
  152.       BackStyle       =   0  'Transparent
  153.       Caption         =   "Align Option:"
  154.       Height          =   195
  155.       Index           =   1
  156.       Left            =   60
  157.       TabIndex        =   8
  158.       Top             =   3600
  159.       Width           =   915
  160.    End
  161.    Begin VB.Label Label1 
  162.       BackStyle       =   0  'Transparent
  163.       Caption         =   "Select a Motif:"
  164.       Height          =   195
  165.       Index           =   0
  166.       Left            =   60
  167.       TabIndex        =   6
  168.       Top             =   1440
  169.       Width           =   4635
  170.    End
  171. Attribute VB_Name = "frmPlayMotif"
  172. Attribute VB_GlobalNameSpace = False
  173. Attribute VB_Creatable = False
  174. Attribute VB_PredeclaredId = True
  175. Attribute VB_Exposed = False
  176. Option Explicit
  177. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  178. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  179. '  File:       frmPlayMotif.frm
  180. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  181. Implements DirectXEvent8
  182. Private Type Motif_Node
  183.     Motif As DirectMusicSegment8
  184.     Name As String
  185.     ListIndex As Long
  186. End Type
  187. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  188. Private dx As New DirectX8
  189. Private dmPerf As DirectMusicPerformance8
  190. Private dmLoader As DirectMusicLoader8
  191. Private dmSegment As DirectMusicSegment8
  192. Private mlSeg As Long
  193. Private moMotifs() As Motif_Node
  194. Private Sub cmdExit_Click()
  195.     Unload Me
  196. End Sub
  197. Private Sub cmdPlay_Click()
  198.     If chkLoop.Value = vbChecked Then
  199.         dmSegment.SetRepeats INFINITE
  200.     Else
  201.         dmSegment.SetRepeats 0
  202.     End If
  203.     dmPerf.PlaySegmentEx dmSegment, 0, 0
  204.     EnablePlayUI False
  205. End Sub
  206. Private Sub cmdPlayMotif_Click()
  207.     Dim lFlags As CONST_DMUS_SEGF_FLAGS
  208.     lFlags = DMUS_SEGF_SECONDARY
  209.     If optBeat.Value Then lFlags = lFlags Or DMUS_SEGF_BEAT
  210.     If optDefault.Value Then lFlags = lFlags Or DMUS_SEGF_DEFAULT
  211.     If optGrid.Value Then lFlags = lFlags Or DMUS_SEGF_GRID
  212.     If optImmediate.Value Then lFlags = lFlags Or DMUS_SEGF_SECONDARY
  213.     If optMeasure.Value Then lFlags = lFlags Or DMUS_SEGF_MEASURE
  214.     dmPerf.PlaySegmentEx moMotifs(lstMotif.ListIndex).Motif, lFlags, 0
  215. End Sub
  216. Private Sub cmdSegment_Click()
  217.     Static sCurDir As String
  218.     Static lFilter As Long
  219.     'We want to open a file now
  220.     cdlOpen.flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
  221.     cdlOpen.FilterIndex = lFilter
  222.     cdlOpen.Filter = "Segment Files (*.sgt)|*.sgt"
  223.     cdlOpen.FileName = vbNullString
  224.     If sCurDir = vbNullString Then
  225.         'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  226.         Dim sWindir As String
  227.         sWindir = Space$(255)
  228.         If GetWindowsDirectory(sWindir, 255) = 0 Then
  229.             'We couldn't get the windows folder for some reason, use the c:\
  230.             cdlOpen.InitDir = "C:\"
  231.         Else
  232.             Dim sMedia As String
  233.             sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  234.             If Right$(sWindir, 1) = "\" Then
  235.                 sMedia = sWindir & "Media"
  236.             Else
  237.                 sMedia = sWindir & "\Media"
  238.             End If
  239.             If Dir$(sMedia, vbDirectory) <> vbNullString Then
  240.                 cdlOpen.InitDir = sMedia
  241.             Else
  242.                 cdlOpen.InitDir = sWindir
  243.             End If
  244.         End If
  245.     Else
  246.         cdlOpen.InitDir = sCurDir
  247.     End If
  248.     On Local Error GoTo ClickedCancel
  249.     cdlOpen.CancelError = True
  250.     cdlOpen.ShowOpen   ' Display the Open dialog box
  251.     'Save the current information
  252.     sCurDir = GetFolder(cdlOpen.FileName)
  253.     'Set the search folder to this one so we can auto download anything we need
  254.     dmLoader.SetSearchDirectory sCurDir
  255.     lFilter = cdlOpen.FilterIndex
  256.             
  257.     On Local Error GoTo NoLoadSegment
  258.     'Before we load the segment stop one if it's playing
  259.     cmdStop_Click
  260.     'Now let's load the segment
  261.     LoadSegment cdlOpen.FileName
  262.     Exit Sub
  263. NoLoadSegment:
  264.     UpdateStatus "Couldn't load this segment"
  265. ClickedCancel:
  266. End Sub
  267. Private Sub cmdStop_Click()
  268.     'Stop the segment
  269.     dmPerf.StopEx dmSegment, 0, 0
  270.     EnablePlayUI True
  271.     UpdateStatus "User pressed stop."
  272. End Sub
  273. Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
  274.     'Here we will handle the DMusic callbacks
  275.     Dim dmNotification As DMUS_NOTIFICATION_PMSG
  276.     Dim oState As DirectMusicSegmentState8
  277.     Dim oSeg As DirectMusicSegment8
  278.     Dim lCount As Long
  279.     On Error GoTo FailedOut
  280.     'Process all events
  281.     Do While dmPerf.GetNotificationPMSG(dmNotification)
  282.         If dmNotification.lNotificationOption = DMUS_NOTIFICATION_SEGEND Then 'The segment has ended
  283.             'First we need to figure out which segment
  284.             Set oState = dmNotification.User 'The user field holds the segment state on segment notifications
  285.             Set oSeg = oState.GetSegment 'Get the segment from the state
  286.             'Is this the primary segment?
  287.             If oSeg Is dmSegment Then 'Yup
  288.                 UpdateStatus "Primary Segment stopped playing."
  289.                 EnablePlayUI True
  290.             Else
  291.                 'Go through all of the other segments
  292.                 For lCount = 0 To UBound(moMotifs)
  293.                     If oSeg Is moMotifs(lCount).Motif Then
  294.                         UpdateStatus moMotifs(lCount).Name & " motif stopped playing."
  295.                         'Now update the listbox
  296.                         lstMotif.List(moMotifs(lCount).ListIndex) = moMotifs(lCount).Name
  297.                     End If
  298.                 Next
  299.             End If
  300.         End If
  301.         If dmNotification.lNotificationOption = DMUS_NOTIFICATION_SEGSTART Then 'The segment has started
  302.             'First we need to figure out which segment
  303.             Set oState = dmNotification.User 'The user field holds the segment state on segment notifications
  304.             Set oSeg = oState.GetSegment 'Get the segment from the state
  305.             'Is this the primary segment?
  306.             If oSeg Is dmSegment Then 'Yup
  307.                 UpdateStatus "Primary Segment started playing."
  308.             Else
  309.                 'Go through all of the other segments
  310.                 For lCount = 0 To UBound(moMotifs)
  311.                     If oSeg Is moMotifs(lCount).Motif Then
  312.                         UpdateStatus moMotifs(lCount).Name & " motif started playing."
  313.                         'Now update the listbox
  314.                         lstMotif.List(moMotifs(lCount).ListIndex) = moMotifs(lCount).Name & " (Playing)"
  315.                     End If
  316.                 Next
  317.             End If
  318.         End If
  319.     Loop
  320.     Exit Sub
  321. FailedOut:
  322.     MsgBox "Error processing this Notification", vbOKOnly Or vbInformation, "Cannot Process."
  323. End Sub
  324. Private Sub Form_Load()
  325.     Me.Show
  326.     InitAudio
  327. End Sub
  328. Private Sub InitAudio()
  329.     On Error GoTo FailedInit
  330.     Dim dma As DMUS_AUDIOPARAMS
  331.     Dim sMedia As String
  332.     'Create our objects
  333.     Set dmPerf = dx.DirectMusicPerformanceCreate
  334.     Set dmLoader = dx.DirectMusicLoaderCreate
  335.     'Set up a default audio path
  336.     dmPerf.InitAudio Me.hWnd, DMUS_AUDIOF_ALL, dma, , DMUS_APATH_SHARED_STEREOPLUSREVERB, 128
  337.     'Create an event handle
  338.     mlSeg = dx.CreateEvent(Me)
  339.     dmPerf.AddNotificationType DMUS_NOTIFY_ON_SEGMENT
  340.     dmPerf.SetNotificationHandle mlSeg
  341.     'Don't let them play a motif yet
  342.     cmdPlayMotif.Enabled = False
  343.     'Now let's load our default segment
  344.     sMedia = FindMediaDir("sample.sgt")
  345.     dmLoader.SetSearchDirectory sMedia
  346.     If sMedia = vbNullString Then sMedia = AddDirSep(CurDir)
  347.     LoadSegment sMedia & "sample.sgt"
  348.     EnablePlayMotif False
  349.     Exit Sub
  350. FailedInit:
  351.     MsgBox "Could not initialize DirectMusic." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  352.     Unload Me
  353. End Sub
  354. Private Sub Cleanup()
  355.     On Error Resume Next
  356.     'Get rid of our event
  357.     dmPerf.RemoveNotificationType DMUS_NOTIFY_ON_SEGMENT
  358.     dx.DestroyEvent mlSeg
  359.     'Unload our segment
  360.     If Not (dmSegment Is Nothing) Then dmSegment.Unload dmPerf.GetDefaultAudioPath
  361.     Set dmSegment = Nothing
  362.     'Get rid of our motifs
  363.     ReDim moMotifs(0)
  364.     'Cleanup
  365.     dmPerf.CloseDown
  366.     Set dmPerf = Nothing
  367.     Set dmLoader = Nothing
  368. End Sub
  369. Private Sub Form_Unload(Cancel As Integer)
  370.     Cleanup
  371. End Sub
  372. Private Function GetFolder(ByVal sFile As String) As String
  373.     Dim lCount As Long
  374.     For lCount = Len(sFile) To 1 Step -1
  375.         If Mid$(sFile, lCount, 1) = "\" Then
  376.             GetFolder = Left$(sFile, lCount)
  377.             Exit Function
  378.         End If
  379.     Next
  380.     GetFolder = vbNullString
  381. End Function
  382. Public Sub EnablePlayUI(fEnable As Boolean)
  383.     'Enable/Disable the buttons
  384.     If fEnable Then
  385.         chkLoop.Enabled = True
  386.         cmdStop.Enabled = False
  387.         cmdPlay.Enabled = True
  388.         cmdSegment.Enabled = True
  389.         cmdPlay.SetFocus
  390.     Else
  391.         chkLoop.Enabled = False
  392.         cmdStop.Enabled = True
  393.         cmdPlay.Enabled = False
  394.         cmdSegment.Enabled = False
  395.         cmdStop.SetFocus
  396.     End If
  397.     If lstMotif.ListCount > 0 And lstMotif.ListIndex <> -1 Then
  398.         EnablePlayMotif Not fEnable
  399.     Else
  400.         EnablePlayMotif False
  401.     End If
  402. End Sub
  403. Public Sub EnablePlayMotif(ByVal fEnable As Boolean)
  404.     cmdPlayMotif.Enabled = fEnable
  405. End Sub
  406. Private Sub LoadSegment(ByVal sFile As String)
  407.     Dim lTrack As Long, lCount As Long
  408.     Dim oStyle As DirectMusicStyle8
  409.     Dim lTotalStyle As Long, lTempTotalStyle As Long
  410.     On Error GoTo LeaveProc
  411.     ReDim moMotifs(0)
  412.     lstMotif.Clear
  413.     Set dmSegment = dmLoader.LoadSegment(sFile)
  414.     dmSegment.Download dmPerf.GetDefaultAudioPath
  415.     txtSegment.Text = sFile
  416.     EnablePlayUI True
  417.     'Now let's get the motifs in this segment
  418.     Do While True
  419.         Set oStyle = dmSegment.GetStyle(lTrack)
  420.         lTotalStyle = lTotalStyle + oStyle.GetMotifCount - 1
  421.         ReDim Preserve moMotifs(lTotalStyle)
  422.         For lCount = 0 To oStyle.GetMotifCount - 1
  423.             lstMotif.AddItem oStyle.GetMotifName(lCount)
  424.             Set moMotifs(lTempTotalStyle + lCount).Motif = oStyle.GetMotif(oStyle.GetMotifName(lCount))
  425.             moMotifs(lTempTotalStyle + lCount).Name = oStyle.GetMotifName(lCount)
  426.             moMotifs(lTempTotalStyle + lCount).ListIndex = lstMotif.ListCount - 1
  427.         Next
  428.         lTrack = lTrack + 1
  429.         lTempTotalStyle = lTotalStyle
  430.     Loop
  431. LeaveProc:
  432.     If lstMotif.ListCount > 0 Then lstMotif.ListIndex = 0
  433.     UpdateStatus "File loaded."
  434. End Sub
  435. Private Sub UpdateStatus(sStat As String)
  436.     txtStatus.Text = sStat
  437. End Sub
  438.